import Vue from "vue";
import Router from "vue-router";
import Layout from "./views/home";
import Login from "./views/login.vue";
import User from "./views/admin/user";
import Role from "./views/admin/role";
import Menu from "./views/admin/menu";
import Home from "./views/home/home";

import store from "./store";
import { validatenull } from "./utils/validate";

Vue.use(Router);

const router = new Router({
  mode: "history",
  base: process.env.BASE_URL,
  routes: [
    {
      path: "/",
      redirect: "/login"
    },
    {
      path: "/login",
      name: "login",
      component: Login,
      meta: {
        isTab: false
      }
    },
    {
      path: "/home",
      redirect: "/home/index",
      name: "home",
      component: Layout,
      meta: {
        isTab: false
      },
      children: [
        {
          name: "首页",
          path: "index",
          component: Home
        },
        {
          name: "用户管理",
          path: "/admin/user",
          component: User
        },
        {
          name: "角色管理",
          path: "/admin/role",
          component: Role
        },
        {
          name: "菜单管理",
          path: "/admin/menu",
          component: Menu
        }
      ]
    }
  ]
});

router.beforeEach((to, from, next) => {
  const meta = to.meta || {};
  const value = to.fullPath;
  const label = to.name;
  if (meta.isTab !== false && !validatenull(value) && !validatenull(label)) {
    store.commit("ADD_TAG", {
      label: label,
      value: value
    });
  }
  next();
});
export default router;
